package main

import (
	"context"
	"demo_v2/message"
	"errors"
	"fmt"
	"github.com/micro/go-micro/v2"
	"github.com/micro/go-plugins/registry/consul/v2"
)
/**
 go-micro v2.0版本：
 protoc compiler：https://github.com/protocolbuffers/protobuf/releases
 protoc-gen-go：go get github.com/golang/protobuf
 protoc-gen-micro:go get github.com/micro/micro/v2/cmd/protoc-gen-micro@master
 go get github.com/micri/go-micro/v2
 go get github.com/micro/go-plugins/registry/consul/v2 [v2版本插件需独自下载对应版本]
 protoc  --proto_path=. --micro_out=. --go_out=. message.proto

 v3版本官方开发中待实现！
 */
type StudentManager struct {}

func (h *StudentManager) GetStudent(ctx context.Context, in *message.StudentRequest, out *message.Student) error {
	fmt.Println("接收到client端请求...")
	studentMap := map[string]message.Student{
		"jimmy":message.Student{Name:"jimmy",Classes:"二年一班",Grade:65},
		"ztind":message.Student{Name:"ztind",Classes:"二年二班",Grade:88},
		"jack":message.Student{Name:"jack",Classes:"二年三班",Grade:100},
	}
	student := studentMap[in.Name]
	if student.Name!=""{
		*out = student
		return nil
	}
	return errors.New(in.Name + "not exists")
}
func main(){
	//创建一个服务
	service := micro.NewService(
		micro.Name("student-server"),
		micro.Version("v1.0.0"),
		micro.Registry(consul.NewRegistry()),//将服务注册到consul服务注册发现组件
	)
	//初始化服务的option配置
	service.Init()
	//注册服务处理
	////注意：Cannot use 'service.Server()' (type "github.com/micro/go-micro/server".Serve 出现这种错误到gopath/pkg/mod里删除重复的包即可
	message.RegisterSetudenServiceHandler(service.Server(),&StudentManager{})
	//运行服务
	service.Run()
}